package com.cvs.zerox.controller;

import com.cvs.zerox.pojo.Fruit;
import com.cvs.zerox.service.FruitService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class FruitController {
    @Autowired
    private FruitService fruitService;

    @GetMapping("/List")
    public String list(Model model) {
        List<Fruit> fruitList = fruitService.selectAll();
        model.addAttribute("list", fruitList);
        return "list";
    }

    @GetMapping("/Detail/{fruitId}")
    public String detail(@PathVariable("fruitId") Integer fruitId, Model model) {
        Fruit fruit = fruitService.detail(fruitId);
        model.addAttribute("fruit", fruit);
        return "detail";
    }

    @PostMapping("/delete")
    @ResponseBody
    public String delete(Model model,@Param("fruitId") Integer fruitId){
        int result = fruitService.delete(fruitId);
        model.addAttribute("fruit",result);
        return result>0?"true":"false";
    }
}
